// view/index/suyuan/suyuan.js
let app = getApp()
let yuming = app.globalData.yuming
var shuzong = []
var shu = []
const request = require('../../config/request');
Page({
  ...request,
  /**
   * 页面的初始数据
   */
  data: {
    yuming: yuming,
    heights: "",
    title: '溯源追踪',
    huan: '',
    currtab: 0,
    // date:'时间筛选',//开始
    yl: [], //原料
    gys: [], //供应商
    page: 1,
    date: '开始时间', //开始
    endTime: '结束时间',
    starTime: '',
    endTimes: '',


     // 搜素
    //  keywordsone:"",
    //  start: '开始时间',
    //  endtime: '结束时间',
    //  begintime: '',
    //  endtimes: '',
    
    istime: false, //弹出时间筛选
    more: false,
    wu: false,
    gongwu: false,
    jincun_wu: false,
    keywords: '',
    yiyao: '',
    jin_kucun: [],
    is_xiao: false,
    xiaosh_list: [],
    yao_bian: '',
    is_dian: 0,

keywordsonde:'',
    is_shai: false,
    yao_ming: '',
    study_status: '',
    yao_pihao: '',
    yao_guige: '',
    yao_fenl: '',
    yao_scdw: '',
    yao_gong: '',
    yao_cai: '',
    yao_jinnum: '',
    yao_jintime: '',
    yao_youx: '',
    yap_piwenh: '',
    isxuan: false,
    isxuans: false,
    jinhuo_shijian: '请选择购进时间',
    jinhuo_shijians: '',
    shengchan_shijian: '请选择生产日期',
    shengchan_shijians: '',
    youxiao_shijian: '请选择有效期',
    youxiao_shijians: '',
    gong_name: '', //供应商名称
    gong_bianh: '', //供应商编号
    gong_faren: '', //法人负责人
    gong_phone: '', //联系电话
    gong_yydm: '', //营业执照信用代码
    gong_xkdm: '', //许可证信用代码
    gong_yyqz: '请选择取证时间', //营业执照取证
    gong_yyqzs: '', //
    gong_xukqz: '请选择取证时间', //许可证取证
    gong_xukqzs: '', //许可证取证
    gong_xukyxq: '请选择有效期', //许可有效期
    gong_xukyxqs: '请选择有效期', //许可有效期
  },
  quxiao() {
    this.setData({
      // is_shai: false,
      yao_ming: '',
      study_status: '',
      yao_pihao: '',
      yao_guige: '',
      yao_fenl: '',
      yao_scdw: '',
      yao_gong: '',
      yao_cai: '',
      yao_jinnum: '',
      yao_jintime: '',
      yao_youx: '',
      yap_piwenh: '',
      isxuan: false,
      isxuans: false,
      jinhuo_shijian: '请选择购进时间',
      jinhuo_shijians: '',
      shengchan_shijian: '请选择生产日期',
      shengchan_shijians: '',
      youxiao_shijian: '请选择有效期',
      youxiao_shijians: '',
      gong_name: '', //供应商名称
      gong_bianh: '', //供应商编号
      gong_faren: '', //法人负责人
      gong_phone: '', //联系电话
      gong_yydm: '', //营业执照信用代码
      gong_xkdm: '', //许可证信用代码
      gong_yyqz: '请选择取证时间', //营业执照取证
      gong_yyqzs: '', //
      gong_xukqz: '请选择取证时间', //许可证取证
      gong_xukqzs: '', //许可证取证
      gong_xukyxq: '请选择有效期', //许可有效期
      gong_xukyxqs: '请选择有效期', //许可有效期
    })
  },
  //许可有效期
  bindyxq(e) {
    this.setData({
      gong_xukyxq: e.detail.value,
      gong_xukyxqs: e.detail.value,
    })
  },
  //许可证取证
  bindxqz(e) {
    this.setData({
      gong_xukqz: e.detail.value,
      gong_xukqzs: e.detail.value,
    })
  },
  //营业执照取证
  bindyyq(e) {
    this.setData({
      gong_yyqz: e.detail.value,
      gong_yyqzs: e.detail.value,
    })
  },
  quedi(e) {
    this.setData({
      is_shai: false
    })
    this.jinxiao()
  },
  // 有效期
  bindyoux(e) {
    this.setData({
      youxiao_shijian: e.detail.value,
      youxiao_shijians: e.detail.value
    })
  },
  // 生产日期
  bindshangc(e) {
    this.setData({
      shengchan_shijian: e.detail.value,
      shengchan_shijians: e.detail.value,
    })
  },
  // 进货时间
  bindjinhuo(e) {
    this.setData({
      jinhuo_shijian: e.detail.value,
      jinhuo_shijians: e.detail.value,
    })
  },
  sou_input(e) {
    let val = e.detail.value
    let modal = e.currentTarget.dataset.modal
    this.setData({
      [modal]: val
    })
  },
  xuanz(e) {
    var index = e.currentTarget.dataset.index
    this.setData({
      study_status: index
    })
    if (index == 1) {
      this.setData({
        isxuan: true,
        isxuans: false,
      })
    }
    if (index == 0) {
      this.setData({
        isxuans: true,
        isxuan: false,
      })
    }
  },
  shaix() {
    this.setData({
      is_shai: true
    })
  },
  yin_shaix() {
    this.setData({
      is_shai: false
    })
  },
  // 进销存修改供应商
  bian_jingong(e) {
    var item = e.currentTarget.dataset.item;
    wx.setStorageSync('gongying_xq', item)
    wx.navigateTo({
      url: '/wenshidu/jin_gong/jin_gong',
    })
  },
  // editII(e){
  //   var item = e.currentTarget.dataset.item;
  //   var id = e.currentTarget.dataset.id
  //   var xiu =  e.currentTarget.dataset.xiu
  //   console.log(item,e);
  //   wx.navigateTo({
  //     url: '/page_one/addyuanliao/addyuanliao?item' + item,
  //   })
  // },
  // 存详情
  cunxq(e) {
    wx.navigateTo({
      url: '/wenshidu/xiaonxq/xiaonxq',
    })
  },
  // 进货列表点击进入库存
  jin_xq(e) {
    var bian = e.currentTarget.dataset.bian;
    this.setData({
      yao_bian: bian,
      page: 1,
      currtab: 2,
      is_dian: 1
    })
    this.jinxiao()
  },
  // 库存列表点击进销售
  xiao_xq(e) {
    shuzong = []
    shu = []
    var bian = e.currentTarget.dataset.bian;
    var drugs = e.currentTarget.dataset.drugs;
    wx.navigateTo({
      url: '/wenshidu/xiaonxq/xiaonxq?bian=' + bian + "&drugs=" + drugs + "&state=0",
    })
    // this.setData({
    //   yao_bian: bian,
    //   currtab: 1,
    //   is_dian: 1
    // })
    // this.jinxiao()
  },
  sousuoaa(e) {
    this.setData({
      keywords: e.detail.value,
      page: 1,
    })
    shuzong = []
    if (this.data.yiyao != 6) {
      this.gyslb()
    } else {
      this.jinxiao()
    }
  },
  // gysxq供应商详情
  gysxq(e) {
    wx.setStorageSync('gongying_xq', e.currentTarget.dataset.item)
    wx.navigateTo({
      url: '/page_one/yiyao_gong_xq/yiyao_gong_xq',
    })
  },
  tan() {
    this.setData({
      istime: !this.data.istime
    })
  },
  que() {
    if (this.data.yiyao != 6) {
      this.gyslb()
    } else {
      this.jinxiao()
    }
    this.setData({
      istime: !this.data.istime
    })
  },
  // 删除员工
  delete(e) {
    var id = e.currentTarget.dataset.id
    var that = this
    wx.showModal({
      title: '您确定要删除吗？',
      cancelText: '否',
      confirmText: '是',
      success(res) {
        if (res.confirm) {
          console.log("是")
          that.shan(id)
        } else {
          console.log("否")
        }
      }
    })
  },
  shan(id) {
    var that = this
    var url = '';
    if (that.data.currtab == 0) {
      url = 'shop/shopDelPurchase'
    }
    if (that.data.currtab == 1) {
      url = 'shop/ShopDelSupplier'
    }
    this.myRequest(yuming + url, {
      id: id,
      shopId: wx.getStorageSync('shopId')
    }, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        if (that.data.yiyao != 6) {
          that.gyslb()
        } else {
          that.jinxiao()
        }
      }
    })
  },
  // 放大
  fangda(e) {
    var img = e.currentTarget.dataset.img
    wx.previewImage({
      urls: [img]
    })
  },
  fangdas(e) {
    var img = e.currentTarget.dataset.img
    var list = e.currentTarget.dataset.list
    wx.previewImage({
      current: img,
      urls: list
    })
  },
  // 查看更多
  lookmore() {
    this.setData({
      page: (this.data.page) + 1
    })

    if (this.data.yiyao != 6) {
      if (this.data.currtab == 0) {
        shuzong = this.data.yl
      } else {
        shuzong = this.data.gys
      }
      this.gyslb()
    } else {
      if (this.data.currtab == 0) {
        shuzong = this.data.yl
      }
      if (this.data.currtab == 1) {
        shuzong = this.data.xiaosh_list
      }
      if (this.data.currtab == 2) {
        shuzong = this.data.jin_kucun
      }
      if (this.data.currtab == 3) {
        shuzong = this.data.gys
      }
      this.jinxiao()
    }
  },
  keywords(e) {
    // console.log(e)
    var key = e.detail.value
    this.setData({
      keywords: key
    })
    this.gyslb()
  },
  keywordsonde(e){
    var key = e.detail.value
    this.setData({
      keywordsonde: key
    })
    this.gyslb()
  },
  // 供应商列表接口
  gyslb() {
    var that = this
    var url = ''
    let data = {}
    if (that.data.currtab == 0) {
      url = 'shop/shopSelectPurchase'
      data = {
        shopId: wx.getStorageSync('shopId'),
        page: that.data.page,
        starTime: that.data.starTime,
        endTime: that.data.endTimes,
        keywords: that.data.keywordsonde
      }
    }
    if (that.data.currtab == 1) {
      url = 'shop/ShopSelectSupplier'
      data = {
        addid: 0,
        shopId: wx.getStorageSync('shopId'),
        page: that.data.page,
        starTime: that.data.starTime,
        endTime: that.data.endTimes,
        keywords: that.data.keywords
      }
    }
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    this.myRequest(yuming + url, data, "POST").then(res => {
      wx.hideLoading();
      shu = res.data.data
      if (that.data.page != 1) {
        if (shu.length != 10) {
          wx.showToast({
            title: '没有更多数据了~',
            icon: "none"
          })
          if (that.data.currtab == 0) {
            that.setData({
              wu: true
            })
          }
          // if (that.data.currtab == 2) {
          //   that.setData({
          //     jincun_wu: true
          //   })
          // }
          if (that.data.currtab == 1) {
            that.setData({
              gongwu: true
            })
          }
        }
      }
      if (that.data.currtab == 0) {
        that.setData({
          yl: shuzong.concat(shu),
          ylength: shuzong.length
        })
      } else {
        that.setData({
          gys: shuzong.concat(shu)
        })
      }
    })
  },
  xinzeng(e) {
    let state = e.currentTarget.dataset.currtab
    let addid = e.currentTarget.dataset.addid
    if (state == 0) {
      wx.navigateTo({
        url: '/page_one/addyuanliao/addyuanliao',
      })
    } else {
      wx.navigateTo({
        url: '/page_one/addgys/addgys?addid=' + addid + '&addtitle=' + '报备供应商',
      })
    }
  },
  // // 选择开始时间
  // bindDateChange: function(e) {
  //   console.log('picker发送选择改变，携带值为', e.detail.value)
  //   this.setData({
  //     date: e.detail.value
  //   })
  // },
  // 选择开始时间
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value,
      starTime: e.detail.value
    })
    this.gyslb()
  },
  // 选择结束时间
  endTime: function (e) {
    this.setData({
      endTime: e.detail.value,
      endTimes: e.detail.value,
    })
    this.gyslb()

  },

  chonzghia(){
    this.setData({
      keywordsonde: '',
      endTimes: '',
      date: '开始时间',
      endTime: '结束时间',
      starTime:'',
    })
    this.gyslb()

  },

  qies(e) {
    shuzong = []
    shu = []
    var index = e.currentTarget.dataset.currtab
    this.setData({
      currtab: index,
      page: 1,
      yl: [],
      gys: [],
      is_dian: 0,
      starTime: '',
      endtime: '',
      keywords: '',
      is_shai: false,
      yao_ming: '',
      study_status: '',
      yao_pihao: '',
      yao_guige: '',
      yao_fenl: '',
      yao_scdw: '',
      yao_gong: '',
      yao_cai: '',
      yao_jinnum: '',
      yao_jintime: '',
      yao_youx: '',
      yap_piwenh: '',
      isxuan: false,
      isxuans: false,
      jinhuo_shijian: '请选择购进时间',
      jinhuo_shijians: '',
      shengchan_shijian: '请选择生产日期',
      shengchan_shijians: '',
      youxiao_shijian: '请选择有效期',
      youxiao_shijians: '',
      date:'开始时间',
      dates:'',
      endTime:'结束时间',
      endTimes:'',
      keywordsonde:''
    })
    if (this.data.yiyao != 6) {
      this.gyslb()
    } else {
      this.jinxiao()
    }
  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      title: options.title,
      yiyao: wx.getStorageSync('stryId'), //6是医药
    })
    let menuButtonObject = wx.getMenuButtonBoundingClientRect();
    wx.getSystemInfo({
      success: (res) => {
        let statusBarHeight = res.statusBarHeight,
          navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2; //导航高度
        this.setData({
          navHeight: navHeight + 30
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    shuzong = []
    shu = []
    this.setData({
      page: 1,
      wu: false,
      gongwu: false,
      jincun_wu: false,
    })
    if (this.data.yiyao != 6) {
      this.setData({
        huan: ['索证索票', '报备供应商'],
      })
      this.gyslb()
    } else {
      this.setData({
        huan: ['购进', '销售', '库存', '供应商报备'],
      })
      this.jinxiao()
    }
  },
  jinxiao() {
    var that = this
    var url = ''
    let data = {}
    if (that.data.currtab == 0) {
      url = 'Shop/shop_jxc_alllist'
      data = {
        shopId: wx.getStorageSync('shopId'),
        page: that.data.page,
        satrtime: that.data.starTime,
        endtime: that.data.endTimes,
        drugs_name: that.data.yao_ming,
        batch_number: that.data.yao_pihao,
        drugs_specs: that.data.yao_guige,
        drugs_place: that.data.yap_piwenh,
        drugs_prescription: that.data.study_status,
        drugs_class: that.data.yao_gong,
        supplier_name: that.data.yao_jinnum,
        drugs_rktime: that.data.jinhuo_shijians,
        drugs_sctime: that.data.shengchan_shijians,
        drugs_yxtime: that.data.youxiao_shijians,
      }
    }
    if (that.data.currtab == 1) {
      if (that.data.is_dian == 0) {
        url = 'Shop/shop_checkout_alllist'
        data = {
          shopId: wx.getStorageSync('shopId'),
          page: that.data.page,
          satrtime: that.data.starTime,
          endtime: that.data.endTimes,
          keywords: that.data.keywords,
          drugs_name: that.data.yao_ming,
          batch_number: that.data.yao_pihao,
          drugs_specs: that.data.yao_guige,
          drugs_place: that.data.yap_piwenh,
          drugs_prescription: that.data.study_status,
          drugs_class: that.data.yao_gong,
          supplier_name: that.data.yao_jinnum,
          drugs_rktime: that.data.jinhuo_shijians,
          drugs_sctime: that.data.shengchan_shijians,
          drugs_yxtime: that.data.youxiao_shijians,
        }
      }
      if (that.data.is_dian == 1) {
        url = 'Shop/shop_cheaout_xslogs'
        data = {
          shopId: wx.getStorageSync('shopId'),
          page: that.data.page,
          batch_number: that.data.yao_bian
        }
      }
    }
    if (that.data.currtab == 2) {
      // 普通点击tab
      if (that.data.is_dian == 0) {
        url = 'shop/shop_jxckc_alllist'
        data = {
          shopId: wx.getStorageSync('shopId'),
          page: that.data.page,
          keywords: that.data.keywords,
          drugs_name: that.data.yao_ming,
          batch_number: that.data.yao_pihao,
          drugs_specs: that.data.yao_guige,
          drugs_place: that.data.yap_piwenh,
          drugs_prescription: that.data.study_status,
          drugs_class: that.data.yao_gong,
          supplier_name: that.data.yao_jinnum,
          drugs_rktime: that.data.jinhuo_shijians,
          drugs_sctime: that.data.shengchan_shijians,
          drugs_yxtime: that.data.youxiao_shijians,
        }
      }
      // 通过进货点击
      if (that.data.is_dian == 1) {
        url = 'Shop/shop_jxckc_allyiyao'
        data = {
          shopId: wx.getStorageSync('shopId'),
          page: that.data.page,
          number_drugs: that.data.yao_bian,
        }
      }
    }
    if (that.data.currtab == 3) {
      url = 'Shop/shop_gys_alllist'
      data = {
        shopId: wx.getStorageSync('shopId'),
        page: that.data.page,
        supplier_name: that.data.gong_name,
        supplier_number: that.data.gong_bianh,
        supplier_person: that.data.gong_faren,
        supplier_tell: that.data.gong_phone,
        social_credit_code: that.data.gong_yydm,
        supplier_yingye_startime: that.data.gong_yyqz,
        supplier_jingying_card: that.data.gong_xkdm,
        supplier_jingying_startime: that.data.gong_xukqz,
        supplier_jingying_endtime: that.data.gong_xukyxq,
        // keywords: that.data.keywords
      }
    }
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    this.myRequest(yuming + url, data, "POST").then(res => {
      wx.hideLoading();
      shu = res.data.data
      if (that.data.page != 1) {
        if (shu.length != 10) {
          wx.showToast({
            title: '没有更多数据了~',
            icon: "none"
          })
          if (that.data.currtab == 0) {
            that.setData({
              wu: true,
              jinhuo_shijian: '请选择购进时间',
              jinhuo_shijians: '',
              shengchan_shijian: '请选择生产日期',
              shengchan_shijians: '',
              youxiao_shijian: '请选择有效期',
              youxiao_shijians: '',
            })
          }
          if (that.data.currtab == 1) {
            that.setData({
              is_xiao: true
            })
          }
          if (that.data.currtab == 2) {
            that.setData({
              jincun_wu: true
            })
          }
          if (that.data.currtab == 3) {
            that.setData({
              gongwu: true
            })
          }
        }
      }
      if (that.data.currtab == 0) {
        that.setData({
          yl: shuzong.concat(shu),
          jinhuo_shijian: '请选择购进时间',
          jinhuo_shijians: '',
          shengchan_shijian: '请选择生产日期',
          shengchan_shijians: '',
          youxiao_shijian: '请选择有效期',
          youxiao_shijians: '',
        })
      }
      if (that.data.currtab == 1) {
        that.setData({
          xiaosh_list: shuzong.concat(shu)
        })
        console.log(that.data.xiaosh_list)
      }
      if (that.data.currtab == 2) {
        if (that.data.is_dian == 0) {
          var ShopJxc = shu
          var xianyou = 0
          //   if (ShopJxc) {
          //     ShopJxc.forEach(item => {
          //       item.piciArr.forEach(items => {
          //         items.xianyou = items.drugs_stock - items.existing_stock
          //         xianyou += Number(items.xianyou)
          //       })
          //     })
          //   }
          that.setData({
            jin_kucun: shuzong.concat(ShopJxc)
          })
        }
        if (that.data.is_dian == 1) {
          //   var ShopJxc = res.data.data.ShopJxc
          var ShopJxckc = res.data.data
          var xianyou = 0
          //   if (ShopJxc) {
          //     ShopJxc.forEach(item => {
          //       item.xianyou = item.drugs_stock - item.existing_stock
          //       xianyou += Number(item.xianyou)
          //     })
          //   }
          that.setData({
            ShopJxc,
            ShopJxckc,
            xianyou
          })
        }
      }
      if (that.data.currtab == 3) {
        that.setData({
          gys: shuzong.concat(shu)
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})